﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EMoney.IStock.Client.Core;
using EMoney.IStock.Client.Framework;
using EMoney.IStock.Client.Models.QQ;
using ThinkDev.Logging;

namespace EMoney.IStock.Client.Business.QQ
{
    /// <summary>
    /// 排行榜业务处理类
    /// </summary>
    public class RankBll
    {
        #region 高手数据列表 缓存写入
        /// <summary>
        /// 总排行缓存写入
        /// </summary>
        public static void WriteCache_RankLst()
        {
            LogManager.DefaultLogger.Debug("============总排行缓存写入开始===============");
            string qq = ConfigHelper.Default_QQ;
            string key = QQConstDefine.GetRankLst_KEY;
            List<Rank> rankLst = new List<Rank>();
            string yidata = StringHelper.GetYiData(qq);
            Rank rank = new Rank();
            string rankUrl = ConfigHelper.QQUrl + "emoney2014/EmRank/getRank?tp=em&yidata=" + yidata;
            try
            {
                string bRankStr = WebApiHelper.getUrl(rankUrl);
                if (!string.IsNullOrEmpty(bRankStr))
                {
                    Result obj = JsonHelper.ToObject<Result>(bRankStr);
                    if (obj != null)
                    {
                        string tempstr = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);

                        BaseRank bRank = JsonHelper.ToObject<BaseRank>(tempstr);
                        if (bRank != null)
                        {
                            rankLst = bRank.data;
                            if (rankLst != null && rankLst.Count > 0)
                            {
                                rankLst = rankLst.OrderBy(x => x.rank_key).ToList();
                                CacheManager.QQCache.Set<List<Rank>>(key, rankLst, 60 * 1);
                                LogManager.DefaultLogger.Debug("============总排行缓存写入成功[" + rankLst.Count + "]===============");
                            }
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                LogManager.WebApiLogger.Error(LogConvert.ParseWebEx(ex), string.Format("RankBll->WriteCache_RankLst 写入缓存发生异常！[url={0}]", rankUrl));
            }
        }

        /// <summary>
        /// 周收益排行缓存写入
        /// </summary>
        public static void WriteCache_RankWeekLst()
        {
            LogManager.DefaultLogger.Debug("============周收益排行缓存写入开始===============");
            string qq = ConfigHelper.Default_QQ;
            string key = QQConstDefine.GetRankWeekLst_KEY;
            List<Rank> rankLst = new List<Rank>();
            string yidata = StringHelper.GetYiData(qq);
            Rank rank = new Rank();
            string rankUrl = ConfigHelper.QQUrl + "emoney2014/EmRank/getRank?tp=em&yidata=" + yidata;
            try
            {
                string bRankStr = WebApiHelper.getUrl(rankUrl);
                if (!string.IsNullOrEmpty(bRankStr))
                {
                    Result obj = JsonHelper.ToObject<Result>(bRankStr);
                    if (obj != null)
                    {
                        string tempstr = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);

                        BaseRank bRank = JsonHelper.ToObject<BaseRank>(tempstr);
                        if (bRank != null)
                        {
                            rankLst = bRank.data;
                            if (rankLst != null && rankLst.Count > 0)
                            {
                                rankLst = rankLst.OrderBy(x => x.rank_week_key).ToList();
                                CacheManager.QQCache.Set<List<Rank>>(key, rankLst, 60 * 1);
                                LogManager.DefaultLogger.Debug("============周收益排行缓存写入成功[" + rankLst.Count + "]===============");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.WebApiLogger.Error(LogConvert.ParseWebEx(ex), string.Format("RankBll->WriteCache_RankWeekLst 写入缓存发生异常！[url={0}]", rankUrl));
            }
        }
        
        /// <summary>
        /// 日收益排行缓存写入
        /// </summary>
        public static void WriteCache_DayLst()
        {
            LogManager.DefaultLogger.Debug("============日收益排行缓存写入开始===============");
            string qq = ConfigHelper.Default_QQ;
            string key = QQConstDefine.GerRankDayLst_KEY; //"EMoney.IStock.Client.Business.QQ.RankBll.GerRankDayLst";
            List<Rank> lst = new List<Rank>();
            string yidata = StringHelper.GetYiData(qq);
            Rank rank = new Rank();
            string rankUrl = ConfigHelper.QQUrl + "emoney2014/EmRank/getRank?rt=day&tp=em&yidata=" + yidata;
            try
            {
                string bRankStr = WebApiHelper.getUrl(rankUrl);
                if (!string.IsNullOrEmpty(bRankStr))
                {
                    Result obj = JsonHelper.ToObject<Result>(bRankStr);
                    if (obj != null)
                    {
                        string tempstr = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);

                        BaseRank bRank = JsonHelper.ToObject<BaseRank>(tempstr);
                        if (bRank != null)
                        {
                            lst = bRank.data;
                            if (lst != null && lst.Count > 0)
                            {
                                lst = lst.OrderBy(x => x.rank_day_key).ToList();
                                CacheManager.QQCache.Set<List<Rank>>(key, lst, 60 * 1);
                                LogManager.DefaultLogger.Debug("============日收益排行缓存写入成功["+ lst.Count +"]===============");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.WebApiLogger.Error(LogConvert.ParseWebEx(ex), string.Format("RankBll->WriteCache_DayLst 写入缓存发生异常！[url={0}]", rankUrl));
            }
        }
        #endregion

        #region 高手数据列表 缓存读取
        
        /// <summary>
        /// 获取高手数据排行列表信息
        /// </summary>
        /// <returns></returns>
        public List<Rank> GetRankLst()
        {
            string qq = ConfigHelper.Default_QQ;
            string key = QQConstDefine.GetRankLst_KEY; //"EMoney.IStock.Client.Business.QQ.RankBll.GetRankLst";
            List<Rank> rankLst = CacheManager.QQCache.Get<List<Rank>>(key);
            if (rankLst == null)
            {
                WriteCache_RankLst();
                rankLst = CacheManager.QQCache.Get<List<Rank>>(key);
            }
            return rankLst;
        }

        /// <summary>
        /// 周收益排行
        /// </summary>
        /// <returns></returns>
        public List<Rank> GetRankWeekLst()
        {
            string qq = ConfigHelper.Default_QQ;
            string key = QQConstDefine.GetRankWeekLst_KEY; //"EMoney.IStock.Client.Business.QQ.RankBll.GetRankWeekLst";
            List<Rank> lst = CacheManager.QQCache.Get<List<Rank>>(key);
            if (lst == null)
            {
                WriteCache_RankWeekLst();
                lst = CacheManager.QQCache.Get<List<Rank>>(key);
            }
            return lst;
        }
        /// <summary>
        /// 昨日收益排行
        /// </summary>
        /// <returns></returns>
        public List<Rank> GerRankDayLst()
        {
            string qq = ConfigHelper.Default_QQ;
            string key = QQConstDefine.GerRankDayLst_KEY; //"EMoney.IStock.Client.Business.QQ.RankBll.GerRankDayLst";
            List<Rank> lst = CacheManager.QQCache.Get<List<Rank>>(key);
            if (lst == null)
            {
                WriteCache_DayLst();
                lst = CacheManager.QQCache.Get<List<Rank>>(key);
            }
            return lst;
        }

        #endregion
        /// <summary>
        /// 增加订阅
        /// </summary>
        /// <param name="ruid">被订阅的qq号</param>
        /// <param name="qq">订阅的qq号</param>
        /// <returns></returns>
        public string AddEmRss(string ruid,string qq)
        {            
            string result = "-1";
            string yidata = StringHelper.GetYiData(qq);
            string add_emrss_url = ConfigHelper.QQUrl + string.Format("emoney2014/EmRss/add?yidata={0}&ruid={1}", yidata, ruid);
            try
            {
                string add_emrss_str = WebApiHelper.getUrl(add_emrss_url);
                if (!string.IsNullOrEmpty(add_emrss_str))
                {
                    Result obj = JsonHelper.ToObject<Result>(add_emrss_str);
                    if (obj != null)
                    {
                        string tempstr = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);
                        QQCommon qqcom = JsonHelper.ToObject<QQCommon>(tempstr);
                        if (qqcom != null)
                        {
                            result = qqcom.code;
                            string msg = StringHelper.UnicodeToStr(qqcom.msg);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.WebApiLogger.Error(LogConvert.ParseWebEx(ex), string.Format("RankBll->AddEmRss发生异常！[url={0}]", add_emrss_url));
            }
            //删除订阅列表缓存
            string key = "EMoney.IStock.Client.Business.QQ.RankBll.GetEmRssList." + qq;
            CacheManager.QQCache.Remove(key);
            return result;
        }
        /// <summary>
        /// 删除订阅
        /// </summary>
        /// <param name="ruid">被订阅的qq号</param>
        /// <param name="qq">订阅的qq号</param>
        /// <returns></returns>
        public string DelEmRss(string ruid,string qq)
        {
            string result = "-1";
            string yidata =  StringHelper.GetYiData(qq);
            string add_emrss_url = ConfigHelper.QQUrl + string.Format("emoney2014/EmRss/del?yidata={0}&ruid={1}", yidata, ruid);
            try
            {
                string add_emrss_str = WebApiHelper.getUrl(add_emrss_url);
                if (!string.IsNullOrEmpty(add_emrss_str))
                {
                    Result obj = JsonHelper.ToObject<Result>(add_emrss_str);
                    if (obj != null)
                    {
                        string tempstr = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);
                        QQCommon qqcom = JsonHelper.ToObject<QQCommon>(tempstr);
                        if (qqcom != null)
                        {
                            result = qqcom.code;
                            string msg = StringHelper.UnicodeToStr(qqcom.msg);
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                LogManager.WebApiLogger.Error(LogConvert.ParseWebEx(ex), string.Format("RankBll->DelEmRss发生异常！[url={0}]", add_emrss_url));
            }
            //删除订阅列表缓存
            string key = "EMoney.IStock.Client.Business.QQ.RankBll.GetEmRssList." + qq;
            CacheManager.QQCache.Remove(key);
            return result;
        }

        /// <summary>
        /// 获取订阅列表
        /// </summary>
        /// <param name="qq">qq号</param>
        /// <returns></returns>
        public EmRss GetEmRssList(string qq)
        {
            string key = "EMoney.IStock.Client.Business.QQ.RankBll.GetEmRssList." + qq;
            EmRss rmrss = CacheManager.QQCache.Get<EmRss>(key);
            if (rmrss == null)
            {
                string yidata = StringHelper.GetYiData(qq);
                string emrss_url = ConfigHelper.QQUrl + string.Format("emoney2014/EmRss/getList?yidata={0}", yidata);
                try
                {
                    string emrss_str = WebApiHelper.getUrl(emrss_url);
                    if (!string.IsNullOrEmpty(emrss_str))
                    {
                        Result obj = JsonHelper.ToObject<Result>(emrss_str);
                        if (obj != null)
                        {
                            string tempstr = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);
                            rmrss = JsonHelper.ToObject<EmRss>(tempstr);
                            CacheManager.QQCache.Set<EmRss>(key, rmrss, 30);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogManager.WebApiLogger.Error(LogConvert.ParseWebEx(ex), string.Format("RankBll->GetEmRssList发生异常！[url={0}]", emrss_url));
                }
                
            }
            return rmrss;
        }

        /// <summary>
        /// 根据qq获取相应的qq信息（昵称/头像/）
        /// </summary>
        /// <param name="qq"></param>
        /// <returns></returns>
        public QQUserInfo GetQQUserInfo(string qq)
        {
            string key = "EMoney.IStock.Client.Business.QQ.RankBll.GetQQUserInfo." + qq;
            QQUserInfo qquserinfo = CacheManager.QQCache.Get<QQUserInfo>(key);
            if (qquserinfo == null)
            {
                string yidata = StringHelper.GetYiData(qq);
                string userInfo_url = ConfigHelper.QQUrl + string.Format("emoney2014/EmUser/getUserInfo?yidata={0}", yidata);
                try
                {
                    string userInfo_str = WebApiHelper.getUrl(userInfo_url);
                    if (!string.IsNullOrEmpty(userInfo_str))
                    {
                        Result obj = JsonHelper.ToObject<Result>(userInfo_str);
                        if (obj != null)
                        {
                            string tempstr = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);
                            if (!string.IsNullOrEmpty(tempstr))
                            {
                                QQCommon qqcom = JsonHelper.ToObject<QQCommon>(tempstr);
                                if (qqcom != null)
                                {
                                    if (qqcom.code == "0")
                                    {
                                        BaseQQUserInfo baseInfo = JsonHelper.ToObject<BaseQQUserInfo>(tempstr);
                                        if (baseInfo != null)
                                        {
                                            qquserinfo = baseInfo.data;
                                            if (qquserinfo != null)
                                            {
                                                CacheManager.QQCache.Set<QQUserInfo>(key, qquserinfo, 60 * 6);
                                            }
                                        }
                                        else
                                        {

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogManager.WebApiLogger.Error(LogConvert.ParseWebEx(ex), string.Format("RankBll->GetQQUserInfo发生异常！[url={0}]", userInfo_url));
                }
            }
            return qquserinfo;
        }


        /// <summary>
        /// 获取qq用户昵称
        /// </summary>
        /// <returns></returns>
        public string GetNickName(string qq)
        {
            string nickname = "";

            QQUserInfo qquserinfo = GetQQUserInfo(qq);
            if (qquserinfo != null)
            {
                nickname = qquserinfo.nickname;
            }
            return nickname;
        }
       
    }
}


